<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ArticlesController
 *
 * @author Kamdem Kenmogne
 */
class CategorieController extends Controller {
    //put your code here
    
    protected $app;
    
    public function __construct($app) 
    {
        parent::__construct($app);
        $this->app = $app;
    }
    
    /**
     * Creating new categorie in db
     * method POST
     * url - /article/
     */
    public function addCategorie()
    {
        //call the fonction authentificate
//        $this->app->authen->authenticate();
                // check for required params
        $this->app->authen->verifyRequiredParams(array('name', 'description'));
        $response = array();
        
        $name = $this->app->request()->post('name');
        $description = $this->app->request()->post('description');
       
//        global $user_id;
//        $user_id = $this->app->user_id;
        // creating new article
        $categorie_id = $this->app->myDB->createCategorie($name, $description);

        if ($categorie_id == CATEGORIE_CREATED_SUCCESSFULLY) {
            $response["error"] = false;
            $response["message"] = "Article created successfully";
            $response["Categorie_Id"] = $categorie_id;
//            $this->app->authen->echoResponse(201, $response);
        }else if ($categorie_id == CATEGORIE_CREATE_FAILED){
            $response["error"] = true;
            $response["message"] = "Failed to create Categorie. Please try again";
//            $this->app->authen->echoResponse(200, $response);
        } 
        else if ($categorie_id == CATEGORIE_ALREADY_EXISTED){
            $response["error"] = true;
            $response["message"] = "Sorry, this Categories´name already existed";
//            $this->app->authen->echoResponse(200, $response);
        }
        $this->app->authen->echoResponse(201, $response);
    }
    
    /**
     * Updating existing article
     * method PUT
     * url - /article/:id
     */
    public function editCategorie($categorie_id)
    {
        //call the fonction authentificate
//        $this->app->authen->authenticate();
       // check for required params
        $this->app->authen->verifyRequiredParams(array('name', 'description'));
        
        $name = $this->app->request()->put('name');
        $description = $this->app->request()->put('description');
        
//        global $user_id;
        //$user_id = $this->app->user_id;
        // updating article
        $result = $this->app->myDB->updateCategorie($categorie_id, $name, $description) ;
        
        $response = array();
        
        if ($result) {
            // categorie updated successfully
            $response["error"] = false;
            $response["message"] = "article updated successfully";
        } else {
            // categorie failed to update
            $response["error"] = true;
            $response["message"] = "article failed to update. Please try again!";
        }
        $this->app->authen->echoResponse(200, $response); 
    }
    
    /**
     * 
     */
    public function getCategories() 
    {
        $response = array();
        // call user
        $result = $this->app->myDB->getCategories();

        if($result != NULL){
            $this->app->authen->echoResponse(201, $result);
        }
        else{
            $response["error"] = TRUE;
            $response["message"] = "The requested resource doesn't exists";
            $this->app->authen->echoResponse(401, $response);
        }
    }
    
}
